package co.com.javeriana.aes.oms.customer.service;

import co.com.javeriana.aes.oms.common.entity.Customer;
import co.com.javeriana.aes.oms.common.entity.Orders;
import java.util.List;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Jonathan Fernandez <jonathan.fvargas@gmail.com>
 */
@Local(CustomerServiceBeanLocal.class)
@Stateless(name = "CustomerServiceBean", mappedName = "ejb/CustomerServiceBean")
public class CustomerServiceBean implements CustomerServiceBeanLocal {

    @PersistenceContext(unitName = "OMS-PU")
    private EntityManager em;

    @Override
    public List<Customer> getCustomers() {

        List<Customer> customers = null;

        try {
            Query data = em.createNamedQuery("Customer.findAll");
            customers = data.getResultList();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return customers;
    }

    @Override
    public Customer getCustomerById(Integer id) throws javax.persistence.NoResultException {

        Customer customer = null;

        try {
            Query data = em.createNamedQuery("Customer.findByCusid");
            data.setParameter("cusid", id);
            customer = (Customer) data.getSingleResult();
            customer.getOrdersList().size();
            customer.getAddressList().size();
            for (Orders o : customer.getOrdersList()) {
                o.getItemsList().size();
            }
        } catch (javax.persistence.NoResultException e) {
            throw e;
        }
        return customer;
    }

}
